package com.sinog.biz.service.business.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinog.biz.config.param.Converse;
import com.sinog.biz.mapper.havexml.AbAnzhiMapper;
import com.sinog.biz.mapper.havexml.AbPrisonerMapper;
import com.sinog.biz.mapper.havexml.PlacementBaseInfoMapper;
import com.sinog.biz.mapper.havexml.PrisonerFxMapper;
import com.sinog.biz.mapper.havexml.PrisonersZyjnMapper;
import com.sinog.biz.mapper.havexml.SocialEmploymentMapper;
import com.sinog.biz.mapper.havexml.SocialTrainingMapper;
import com.sinog.biz.mapper.havexml.SystemCodeMapper;
import com.sinog.biz.mapper.havexml.UploadAppendixFileMapper;
import com.sinog.biz.service.business.AzgzInfoMainService;
import com.sinog.biz.util.CodeUrlUtils;
import com.sinog.biz.util.IdGen;
import com.sinog.core.domain.AbAnzhiResultVo;
import com.sinog.core.domain.Jyxxmk;
import com.sinog.core.domain.PlacementBaseInfo;
import com.sinog.core.domain.PrisonerFx;
import com.sinog.core.domain.PrisonersZyjn;
import com.sinog.core.domain.SystemCode;
import com.sinog.core.model.UserDTO;
import com.sinog.core.util.JsonUtil;
import com.sinog.core.util.PageReturnUtil;
import com.sinog.core.util.PhoneUtil;
import com.sinog.core.utilbean.Constants;
import com.sinog.core.utilbean.Result;
import com.sinog.core.utilbean.ReturnCode;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

/**
 * @desc 安置帮教管理 - 安置帮教工作信息管理
 * @author sunsf
 * @date 2020-02-07-16:40
 */
@Service
public class AzgzInfoMainServiceImpl extends ServiceImpl<AbAnzhiMapper,AbAnzhiResultVo> implements AzgzInfoMainService {

    @Resource
    private AbAnzhiMapper abAnzhiMapper;
    @Resource
    private PrisonerFxMapper prisonerFxMapper;
    @Resource
    private PrisonersZyjnMapper prisonersZyjnMapper;
    @Resource
    private SystemCodeMapper systemCodeMapper;
    @Resource
    private SocialEmploymentMapper socialEmploymentMapper;
    @Resource
    private SocialTrainingMapper socialTrainingMapper;
    @Resource
    private PlacementBaseInfoMapper placementBaseInfoMapper;
    @Resource
    private AbPrisonerMapper abPrisonerMapper;
    @Resource
    private UploadAppendixFileMapper uploadAppendixFileMapper;


    /**
     * 安置帮教管理 - 安置工作信息管理 - 待安置 - 列表
     * @param pageIndex 起始页
     * @param pageSize 止页
     * @param querykey 根据服刑人员名称模糊搜索
     * @param departid 当前登录用户的departId
     * @param level 当前登录用户的level
     * @param orgid 登录用户的上级单位
     * @param pinggu 是否重点稳控对象 01:是,02:否
     * @param sortField 排序字段
     * @param sortOrder 排序方式
     * @return String
     */
    @Override
    public String getUnSettleList(Integer pageIndex,Integer pageSize,@Converse(caseIf = "-1", converseTo = "0")
            String querykey,String departid,int level,String orgid,String pinggu,String sortField,String sortOrder) {
        Page<Map<String,String>> page = new Page<>(pageIndex + 1,pageSize);
        List<Map<String,String>> list = abPrisonerMapper.getUnSettleList(page,querykey,departid,level,orgid,pinggu,sortField,sortOrder);
        page = page.setRecords(list);
        return PageReturnUtil.pageReturn(page);
    }

    /**
     * 安置帮教管理 - 安置工作信息管理 - 待安置/已安置 查看所有安置信息
     * @param priNum 服刑人员编号
     * @param departid 当前登录用户的departId
     * @return Result
     */
    @Override
    public Result getAzBaseInfonList(String priNum,String departid) {
        Map<String,Object> result = new HashMap<>(16);
        String flag = CodeUrlUtils.getLocalCodeStartTwo();
        List<Map<String,Object>> list = abAnzhiMapper.getAzBaseInfonList(priNum,flag,departid);
        Map<String,Object> prisonerBaseInfo = abPrisonerMapper.getBaseInfoBypriNum(priNum);
        if (ObjectUtils.isEmpty(prisonerBaseInfo.get("jzdz"))) {
            prisonerBaseInfo.remove("jzdz");
        }
        result.put("azInfoList",list);
        result.put("prisonerBaseInfo",prisonerBaseInfo);
        return Result.success(result);
    }

    /**
     * 安置帮教管理 - 安置工作信息管理 - 待安置/已安置 查看多条安置信息
     * @param azId 安置人员编号
     * @return Result
     */
    @Override
    public Result getAzBaseInfo(String azId) {
        Map<String,Object> settleBaseInfo = abAnzhiMapper.getSettleInfoByazId(azId);
        String certs = "";
        if(null != settleBaseInfo && settleBaseInfo.containsKey(Constants.StrFinalValue.PRINUMUC)) {
            String prinum = (String)settleBaseInfo.get(Constants.StrFinalValue.PRINUMUC);
            certs = prisonersZyjnMapper.getCertsByPrinum(prinum);
        }
        if(null != settleBaseInfo && null != settleBaseInfo.get(Constants.StrFinalValue.JINENG) && !"".equals(settleBaseInfo.get(Constants.StrFinalValue.JINENG))) {
            Jyxxmk jyxxmk;
            jyxxmk = socialEmploymentMapper.selectById(settleBaseInfo.get(Constants.StrFinalValue.JINENG).toString());
            if(null == jyxxmk) {
                jyxxmk = new Jyxxmk();
            }
            settleBaseInfo.put("jinengName",jyxxmk.getZwmc());
        }
        List<Map<String,Object>> appendixList = uploadAppendixFileMapper.getUploadAppendixFile(azId,null,null);
        if(null != settleBaseInfo) {
            settleBaseInfo.put("appendixList",appendixList);
            settleBaseInfo.put("certs",certs);
        }
        return Result.success(settleBaseInfo);
    }

    /**
     * 获取证书信息
     * @param prinum 服刑人员编号
     * @return Result
     */
    @Override
    public Result getCertsByPrinum(String prinum) {
        Map<String,Object> settleBaseInfo = new HashMap<>(16);
        String certs = prisonersZyjnMapper.getCertsByPrinum(prinum);
        //根据服刑编号查出这个服刑人员的技能
        List<PrisonersZyjn> zyjnList = prisonersZyjnMapper.selectList(new QueryWrapper<PrisonersZyjn>().eq("pri_num",prinum)
                                                                                                       .eq("type","-1"));
        settleBaseInfo.put("certs",certs);
        List<String> jimengList = new ArrayList<>(16);
        if(null != zyjnList && !zyjnList.isEmpty()) {
            for(PrisonersZyjn prisonersZyjn : zyjnList) {
                //根据技能id去数据词典表查技能名
                SystemCode systemCode = systemCodeMapper.selectOne(new QueryWrapper<SystemCode>().eq("code_id",prisonersZyjn.getParentZyjn()).eq("code_type","4_1"));
                if(null != systemCode) {
                    jimengList.add(systemCode.getCodeName().replace("人员",""));
                }
            }
            settleBaseInfo.put("jineng",StringUtils.join(jimengList,","));
        }
        return Result.success(settleBaseInfo);
    }

    /**
     * 安置帮教管理 - 安置工作信息管理 - 待安置/已安置 - 安置 - 根据帮教基地类型查询具体条目
     * @param azType 基地类型
     * @param user 当前登录用户
     * @param priNum 服刑人员编号
     * @return List<Map < String,Object>>
     */
    @Override
    public List<Map<String,Object>> getAzjdList(String azType,UserDTO user,String priNum) {
        // 根据罪犯编号获取帮教对象类别
        if(priNum.contains(",")){
            return placementBaseInfoMapper.getAnZhijdList(azType, user.getDepartid());
        }
        PrisonerFx prisonerFx = prisonerFxMapper.selectById(priNum);
        String pinggu = null == prisonerFx ? null : prisonerFx.getPinggu();
        String localCode;
        // 山西个性化功能,基地只有在启用时才能使用
        {
            localCode = CodeUrlUtils.getLocalCode();
        }
        return placementBaseInfoMapper.getAzjdList(azType,user.getDepartid(),null == pinggu ? null : Integer.parseInt(pinggu),priNum,localCode);
    }

    /**
     * 安置帮教管理 - 安置工作信息管理 - 待安置/已安置 - 安置 - 就业推荐列表
     * @param priNum 服刑人员编号
     * @param departid 当前登录用户departId
     * @param level 当前登录用户level
     * @param orgid 当前登录用户的上级单位Id
     * @return List<Map < String,Object>>
     */
    @Override
    public String getJytjList(Integer pageIndex,Integer pageSize,String priNum,String departid,int level,String orgid) {
        Page<Map<String,String>> page = new Page<>(pageIndex + 1,pageSize);
        List<Map<String,String>> list = socialEmploymentMapper.getJytjList(page,priNum,departid,level,orgid);
        page = page.setRecords(list);
        return PageReturnUtil.pageReturn(page);
    }

    /**
     * 安置帮教管理 - 安置工作信息管理 - 待安置/已安置 - 安置 - 保存
     * @param dataStr 业务数据
     * @return Result
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result saveAzInfo(String dataStr) {
        AbAnzhiResultVo abAnzhiResultVo = JsonUtil.toBean(dataStr,AbAnzhiResultVo.class);
        if(null == abAnzhiResultVo.getId() || "".equals(abAnzhiResultVo.getId())) {
            abAnzhiResultVo.setId(IdGen.strid());
        }
        //判断是否有选择了安置基地，如果不存在安置基地则走原来流程
        String priNum = abAnzhiResultVo.getPriNum();
        if(StringUtils.isEmpty(abAnzhiResultVo.getJdid())) {
            boolean tag = abAnzhiResultVo.insertOrUpdate();
            if(tag) {
                abPrisonerMapper.updateState(priNum,Constants.NumberStr.NUM_STR_TWO);
                return Result.success(abAnzhiResultVo.getId());
            }
            return Result.failure(ReturnCode.SAVE_FAILED);
        } else {
            PlacementBaseInfo placementBaseInfo = new PlacementBaseInfo();
            placementBaseInfo.setId(abAnzhiResultVo.getJdid());
            PlacementBaseInfo placementBaseInfo1 = placementBaseInfo.selectById();
            String anzhirenshu = "";
            Object anzhirenshu1 = JsonUtil.toMap(dataStr).get("anzhirenshu");
            if(StringUtils.isEmpty((null == anzhirenshu1) ? null : anzhirenshu1.toString())) {
            } else {
                anzhirenshu = JsonUtil.toMap(dataStr).get("anzhirenshu").toString();
                if(!StringUtils.isEmpty(anzhirenshu)) {
                    //判断安置人数是否大于了可安置人数，如果是则返回
                    String keanzhirenshu = placementBaseInfo1.getKeanzhirenshu();
                    if(Integer.parseInt(null == keanzhirenshu ? Constants.NumberStr.NUM_STR_TEN : keanzhirenshu) < Integer.parseInt(anzhirenshu)) {
                        return Result.failure(ReturnCode.NUMBER_OVERFLOW);
                    }
                }
            }
            if(StringUtils.equals(placementBaseInfo1.getShowStatus(),Constants.NumberStr.NUM_STR_TWO)) {
                return Result.failure(ReturnCode.FORBIDDEN_BASE);
            }
            boolean tag = abAnzhiResultVo.insertOrUpdate();
            if(tag) {
                abPrisonerMapper.updateState(priNum,Constants.NumberStr.NUM_STR_TWO);
                if(!StringUtils.isEmpty(anzhirenshu)) {
                    placementBaseInfo.setAnzhirenshu(anzhirenshu);
                } else {
                    placementBaseInfo.setAnzhirenshu(placementBaseInfo.selectById().getAnzhirenshu());
                }
                placementBaseInfoMapper.updateAzrs(placementBaseInfo.getId(),placementBaseInfo.getAnzhirenshu());
                return Result.success(abAnzhiResultVo.getId());
            }
        }
        return Result.failure(ReturnCode.SAVE_FAILED);
    }

    /**
     * 安置帮教管理 - 安置工作信息管理 - 已安置 - 列表
     * @param pageIndex 起始页
     * @param pageSize 止页
     * @param querykey 根据服刑人员名称模糊搜索
     * @param departid 当前登录用户的departId
     * @param level 当前登录用户的level
     * @param orgid 登录用户的上级单位
     * @param pinggu 是否重点稳控对象 01:是,02:否
     * @param sortField 排序字段
     * @param sortOrder 排序方式
     * @return String
     */
    @Override
    public String getDownSettleList(Integer pageIndex,Integer pageSize,@Converse(caseIf = "-1", converseTo = "0")
            String querykey,String departid,int level,String orgid,String pinggu,String sortField,String sortOrder) {
        Page<Map<String,String>> page = new Page<>(pageIndex + 1,pageSize);
        List<Map<String,String>> downSettleList = abPrisonerMapper.getDownSettleList(page,querykey,departid,level,orgid,pinggu,sortField,sortOrder);
        StringBuffer sb = new StringBuffer(16);
        for(Map<String,String> stringStringMap : downSettleList) {
            if(!StringUtils.isEmpty(stringStringMap.get("azfs"))) {
                String[] azfs = stringStringMap.get("azfs").split(",");
                //如果azfs为多个相同的值可以使用set集合(去重);
                HashSet<String> azqkSet = new HashSet<>(16);
                azqkSet.addAll(Arrays.asList(azfs));
                azqkSet.forEach(e -> {
                    switch(e) {
                        case "01":
                            sb.append("落实责任田,");
                            break;
                        case "02":
                            sb.append("公益性岗位安置,");
                            break;
                        case "03":
                            sb.append("自主创业,");
                            break;
                        case "04":
                            sb.append("从事个体经营,");
                            break;
                        case "05":
                            sb.append("企业和经济实体吸纳就业,");
                            break;
                        case "06":
                            sb.append("其他安置方式,");
                            break;
                        default:
                            sb.append("无,");
                            break;
                    }
                });
                stringStringMap.put("azfs",sb.substring(0,sb.lastIndexOf(",")));
                sb.setLength(0);
            }
        }
        page = page.setRecords(downSettleList);
        return PageReturnUtil.pageReturn(page);
    }

    /**
     * 安置帮教管理 - 安置工作信息管理 - 待安置/已安置 - 安置 - 删除
     * @param delId 安置编号
     * @return Result
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result delAzInfoAction(String delId) {
        AbAnzhiResultVo abAnzhiResultVo = abAnzhiMapper.selectById(delId);
        String priNum = abAnzhiResultVo.getPriNum();
        List<Map<String,String>> jds = abAnzhiMapper.searchAnzhiJd(priNum);
        if(0 < jds.size()) {
            jds.forEach(e -> {
                String jdId = e.get(Constants.StrFinalValue.JDID);
                if(!StringUtils.isEmpty(jdId)) {
                    Integer count = abAnzhiMapper.searchAnzhiByPriNum(priNum,jdId);
                    if(count <= 1) {
                        //获取安置基地的数量
                        Integer num = placementBaseInfoMapper.getJdKeAnZhiNum(e.get(Constants.StrFinalValue.JDID));
                        placementBaseInfoMapper.changeJdNum(e.get("jdid"),--num);
                        //修改历史安置人数
                        PlacementBaseInfo placementBaseInfo = new PlacementBaseInfo();
                        PlacementBaseInfo placement = placementBaseInfo.selectById(e.get(Constants.StrFinalValue.JDID));
                        if(placement != null) {
                            int lsAnzhi = Integer.parseInt(null == placement.getLsAnzhirenshu() ? "0" : placement.getLsAnzhirenshu());
                            placement.setLsAnzhirenshu(String.valueOf(++lsAnzhi));
                            placement.updateById();
                        }
                    }
                }
            });
            int tag = abAnzhiMapper.delAzInfoAction(delId);
            if(tag > 0) {
                return Result.success(ReturnCode.DELETE_SUCCESS);
            }
        }
        return Result.failure(ReturnCode.DELETE_FAILED);
    }

    /**
     * 安置帮教管理 - 安置工作信息管理 - 待安置/已安置 - 安置 - 基地推荐列表
     * @param departid 当前登录用户的departId
     * @param level 当前登录用户的level
     * @param priNum 服刑人员编号
     * @return List<Map < String,Object>>
     */
    @Override
    public String getJdtjList(String departid,int level,String priNum,Integer pageIndex,Integer pageSize) {
        Page<Map<String,String>> page = new Page<>(pageIndex + 1,pageSize);
        // 山西个性化需求,安置帮教基地增加禁用/启用功能
        String localCode;
        {
            localCode = CodeUrlUtils.getLocalCode();
        }
        List<Map<String,String>> jdtjList = placementBaseInfoMapper.getJdtjList(page,departid,level,priNum,localCode);
        //处理基地联系方式
        for(Map<String,String> map : jdtjList) {
            if(null != map.get("dh")) {
                Map<String,String> telAndPhone = PhoneUtil.splitTelAndPhone((String)map.get("dh"));
                map.put("phone",telAndPhone.get("phone"));
                map.put("tel",telAndPhone.get("tel"));
            }
        }
        page = page.setRecords(jdtjList);
        return PageReturnUtil.pageReturn(page);
    }

    /**
     * 获取培训机构下拉列表（获取培训范围是本省、本市、本区县、本街道的所有培训机构）
     * @param user 当前登录用户
     * @return List<Map < String,Object>>
     */
    @Override
    public List<Map<String,Object>> getPxjgList(UserDTO user) {
        return socialTrainingMapper.getPxjgList(user.getDepartid());
    }

    /**
     * 安置帮教管理 - 安置工作信息管理 - 已安置 - 移出基地操作
     * @param priNum 服刑人员编号
     * @return Result
     */
    @Override
    public Result moveJdCheckPriNum(String priNum) {
        List<AbAnzhiResultVo> abAnzhiResultVos = abAnzhiMapper.selectAzByPriNum(priNum);
        if(0 != abAnzhiResultVos.size()) {
            return Result.success(abAnzhiResultVos);
        }
        return Result.failure(ReturnCode.SELECT_FAILED);
    }

    /**
     * 安置帮教管理 - 安置工作信息管理 - 已安置 - 移出基地操作
     * @param priNum 服刑人员编号
     * @return Result
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result changeAnzhiState(String priNum) {
        //获取移出对象的基地,
        List<Map<String, String>> jds = abAnzhiMapper.searchAnzhiJd(priNum);
        // 更新ab_prisoners表状态,删除安置信息
        abPrisonerMapper.updateState(priNum,Constants.NumberStr.NUM_STR_THREE);
        abAnzhiMapper.deleteByAzInfo(priNum);
        if (0 != jds.size()) {
            jds.forEach(e -> {
                String jdId = e.get(Constants.StrFinalValue.JDID);
                if (!StringUtils.isEmpty(jdId)) {
                    Integer count = abAnzhiMapper.searchAnzhiByPriNum(priNum,jdId);
                    if(count <= 1) {
                        //获取安置基地的数量
                        Integer num = placementBaseInfoMapper.getJdKeAnZhiNum(e.get(Constants.StrFinalValue.JDID));
                        placementBaseInfoMapper.changeJdNum(e.get("jdid"), --num);
                        //修改历史安置人数
                        PlacementBaseInfo placementBaseInfo = new PlacementBaseInfo();
                        PlacementBaseInfo placement = placementBaseInfo.selectById(e.get(Constants.StrFinalValue.JDID));
                        if (placement != null) {
                            int lsAnzhi = Integer.parseInt(null == placement.getLsAnzhirenshu() ? "0" : placement.getLsAnzhirenshu());
                            placement.setLsAnzhirenshu(String.valueOf(++lsAnzhi));
                            placement.updateById();
                        }
                    }
                }
            });
            return Result.success();
        }
        return Result.failure(ReturnCode.SAVE_FAILED);
    }

    /**
     * 安置帮教管理 - 安置工作信息管理 - 已安置 - 批量移出基地操作
     * @param priNums 服刑人员编号(多个priNum用"|"拼接)
     * @return Result
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result moveJdCheckPriNumPl(String priNums) {
        String[] split = priNums.split("\\|");
        //用于记录成功的条数
        for(String s : split) {
            Result result = changeAnzhiState(s);
            if(4000 == result.getCode()) {
                return Result.failure(ReturnCode.UPDATE_FAILED);
            }
        }
        return Result.success();
    }

    /**
     * 安置帮教管理 - 安置工作信息管理 - 已安置 - 移除 -移至其他基地
     * @param data 业务数据
     * @return Result
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public synchronized Result moveJdBypriNum(String data) {
        AbAnzhiResultVo abAnzhiResultVo = JsonUtil.toBean(data,AbAnzhiResultVo.class);
        String priNum = abAnzhiResultVo.getPriNum();
        //获取将要安置的基地
        PlacementBaseInfo placementBaseInfo = new PlacementBaseInfo();
        placementBaseInfo.setId(abAnzhiResultVo.getJdid());
        PlacementBaseInfo placementBaseInfo1 = placementBaseInfo.selectById();
        if(StringUtils.equals(placementBaseInfo1.getShowStatus(),Constants.NumberStr.NUM_STR_TWO)) {
            return Result.failure(ReturnCode.FORBIDDEN_BASE);
        }
        int anzhirenshu = Integer.parseInt(placementBaseInfo1.getAnzhirenshu());
        if(Integer.parseInt(placementBaseInfo1.getKeanzhirenshu()) < ++anzhirenshu) {
            return Result.failure(ReturnCode.NUMBER_OVERFLOW);
        }
        //获取移出对象的基地,
        List<Map<String,String>> moveJdid = abAnzhiMapper.searchAnzhiJd(priNum);
        if(0 != moveJdid.size()) {
            moveJdid.forEach(e -> {
                String jdId = e.get(Constants.StrFinalValue.JDID);
                if(!StringUtils.isEmpty(jdId)) {
                    Integer count = abAnzhiMapper.searchAnzhiByPriNum(priNum,jdId);
                    if(count <= 1) {
                        //获取安置基地的数量
                        Integer num = placementBaseInfoMapper.getJdKeAnZhiNum(e.get(Constants.StrFinalValue.JDID));
                        placementBaseInfoMapper.changeJdNum(e.get(Constants.StrFinalValue.JDID),--num);
                        //修改历史安置人数
                        PlacementBaseInfo placementBaseInfo01 = new PlacementBaseInfo();
                        PlacementBaseInfo placement = placementBaseInfo01.selectById(e.get(Constants.StrFinalValue.JDID));
                        if(placement != null) {
                            int lsAnzhi = Integer.parseInt(null == placement.getLsAnzhirenshu()  ? Constants.NumberStr.NUM_STR_ZERO : placement.getLsAnzhirenshu());
                            placement.setLsAnzhirenshu(String.valueOf(++lsAnzhi));
                            placement.updateById();
                        }
                    }
                }
            });
        }
        placementBaseInfo1.setAnzhirenshu(String.valueOf(anzhirenshu));
        placementBaseInfo1.insertOrUpdate();
        Integer flag = abAnzhiMapper.moveJdBypriNum(abAnzhiResultVo.getPriNum(),abAnzhiResultVo.getJdxz(),abAnzhiResultVo.getJdid(),abAnzhiResultVo.getJdanzhi());
        if(flag > 0) {
            return Result.success();
        }
        return Result.failure(ReturnCode.FAILURE);
    }

    /**
     * 安置帮教管理 - 安置工作信息管理 - 已安置 - 移除 - 批量移至其他基地
     * @param data 业务数据
     * @return Result
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result moveJdBypriNumPl(String data) {
        Map<String,Object> map = JsonUtil.toMap(data);
        String pids = (String)map.get("pid");
        String jdid = (String)map.get("jdid");
        PlacementBaseInfo placementBaseInfo = new PlacementBaseInfo();
        PlacementBaseInfo placementBaseInfo1 = placementBaseInfo.selectById(jdid);
        String[] pid = pids.split(",");
        HashSet<String> strings = new HashSet<>();
        for(String s : pid) {
            strings.add(prisonerFxMapper.searchPingguByPriNum(s));
        }
        if(StringUtils.equals(placementBaseInfo1.getJiuzhutype(),Constants.NumberStr.NUM_STR_THREE) || 1 == strings.size()) {
            String[] split = pids.split(",");
            if(!StringUtils.isEmpty(map.get(Constants.StrFinalValue.COUNT).toString())) {
                //如果不等于空，则默认为已经操作过一次，还有一些未迁移
                int num = Integer.parseInt(map.get(Constants.StrFinalValue.COUNT).toString());
                for(int j = num; j < split.length; j++) {
                    map.put(Constants.StrFinalValue.PID,split[j]);
                    Result result = moveJdBypriNum(JsonUtil.toText(map));
                    if(ReturnCode.NUMBER_OVERFLOW.code().equals(result.getCode())) {
                        return Result.failure(ReturnCode.NUMBER_OVERFLOW,num);
                    } else if(ReturnCode.FORBIDDEN_BASE.code().equals(result.getCode())) {
                        return Result.failure(ReturnCode.FORBIDDEN_BASE);
                    }
                    num++;
                }
                return Result.success();
            }
            int count = 0;
            for(String s : split) {
                map.put("pid",s);
                Result result = moveJdBypriNum(JsonUtil.toText(map));
                if(ReturnCode.NUMBER_OVERFLOW.code().equals(result.getCode())) {
                    return Result.failure(ReturnCode.NUMBER_OVERFLOW,count);
                } else if(ReturnCode.FORBIDDEN_BASE.code().equals(result.getCode())) {
                    return Result.failure(ReturnCode.FORBIDDEN_BASE);
                }
                count++;
            }
            return Result.success();
        } else {
            prisonerFxMapper.searchPingguByPriNum(pids.substring(0,pids.indexOf(",")));
        }
        return Result.failure(ReturnCode.SAVE_FAILED);
    }

    /**
     * 获取基地已安置人数
     * @param jdid 基地编号
     * @return Map<String,String>
     */
    @Override
    public Map<String,Integer> selectKeAnZhiNum(String jdid) {
        return placementBaseInfoMapper.selectKeAnZhiNum(jdid);
    }

    /**
     * 获取安置信息列表
     * @param priNum 服刑人员编号
     * @return Result
     */
    @Override
    public Result getAzBaseInfonAgainList(String priNum) {
        Map<String,Object> result = new HashMap<>(16);
        //获取本省份code前两位
        String flag = CodeUrlUtils.getLocalCodeStartTwo();
        List<Map<String,Object>> list = abAnzhiMapper.getAzBaseInfonAgainList(priNum,flag);
        Map<String,Object> prisonerBaseInfo = abPrisonerMapper.getBaseInfoBypriNum(priNum);
        result.put("azInfoList",list);
        result.put("prisonerBaseInfo",prisonerBaseInfo);
        return Result.success(result);
    }

    /**
     * 判断该安置人数是否存在该基地
     * @param priNum 服刑人员编号
     * @param jdid 基地编号
     * @return Integer
     */
    @Override
    public Integer searchAnzhiByPriNum(String priNum,String jdid) {
        return abAnzhiMapper.searchAnzhiByPriNum(priNum,jdid);
    }

    /**
     * 安置帮教管理 - 安置工作信息管理 - 已移出 - 列表
     * @param pageIndex pageIndex
     * @param pageSize pageSize
     * @param querykey querykey
     * @param departId departId
     * @param level level
     * @param orgid orgid
     * @return String
     */
    @Override
    public String getMoveSettleList(Integer pageIndex, Integer pageSize, String querykey, String departId, int level, String orgid,String pinggu,String sortField,String sortOrder) {
        if(StringUtils.equals(querykey, "-1")) {
            querykey= null;
        }
        Page<Map<String,String>> page = new Page<>(pageIndex + 1, pageSize);
        page = page.setRecords(abAnzhiMapper.getMoveSettleList(page, querykey, departId, level, orgid,pinggu,sortField,sortOrder));
        Map<String,Object> result = new HashMap<>(16);
        result.put("total", page.getTotal());
        result.put("data", page.getRecords());
        return JsonUtil.toText(result);
    }
}